package javacore.vch3;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class UnUtf8CharacTest {


    static public String filterOffUtf8Mb4(String text) throws UnsupportedEncodingException {
        byte[] bytes = text.getBytes("utf-8");
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        int i = 0;
        while (i < bytes.length) {
            short b = bytes[i];
            if (b > 0) {
                buffer.put(bytes[i++]);
                continue;
            }
            b += 256;
            if ((b ^ 0xC0) >> 4 == 0) {
                buffer.put(bytes, i, 2);
                i += 2;
            }
            else if ((b ^ 0xE0) >> 4 == 0) {
                buffer.put(bytes, i, 3);
                i += 3;
            }
            else if ((b ^ 0xF0) >> 4 == 0) {
                i += 4;
            }
        }
        buffer.flip();
        return new String(buffer.array(), "utf-8");
    }

    public static String filterOffUtf8Mb4_2(String text) throws UnsupportedEncodingException {
        byte[] bytes = text.getBytes("utf-8");
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        int i = 0;
        while (i < bytes.length) {
            short b = bytes[i];
            if (b > 0) {
                buffer.put(bytes[i++]);
                continue;
            }

            b += 256; //去掉符号位

            if (((b >> 5) ^ 0x06) == 0) {
                buffer.put(bytes, i, 2);
                i += 2;
            } else if (((b >> 4) ^ 0x0E) == 0) {
                buffer.put(bytes, i, 3);
                i += 3;
            } else if (((b >> 3) ^ 0x1E) == 0) {
                i += 4;
            } else if (((b >> 2) ^ 0xBE) == 0) {
                i += 5;
            } else {
                i += 6;
            }
        }
        buffer.flip();
        return new String(buffer.array(), "utf-8");
    }

    public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
        String str1 = "抖音拍照口好好看[打call]http://t.cn/RRbv0ob\"";
        String str2 = "asdassssssssssssa`1234567890-=[];'\\./,~!@#$%^&*()";
        String str3 = "念啥实打实的我去恶趣味看、；’了你😊😊😊😂ψ(｀∇´)ψψ(｀∇´)ψψ(｀∇´)ψψ(｀∇´)ψψ(｀∇´)ψ（￣︶￣）↗　ヾ(≧▽≦*)o¥$₩$€₢₣₦₪₮₱￠④②⑤③⑥⑨⒌⒏⒕⑶⑹⑷▨↘↔↔→↘ㄐㄊㄊㄍ▫⇡▱▮↘^－－㎡㎝㏕₠㎏㎏㎏㎡₥₪";

//        System.out.println(filterOffUtf8Mb4(str1));
//        System.out.println(filterOffUtf8Mb4(str2));
//        System.out.println(filterOffUtf8Mb4(str3));

        System.out.println(filterOffUtf8Mb4_2(str1));
        System.out.println(filterOffUtf8Mb4_2(str2));
        System.out.println(filterOffUtf8Mb4_2(str3));

        System.out.println(DateFormat.getDateInstance().parse("Thu May 23 10:12:39 +0800 2024"));




    }
}
